const Base = require('./base.js');
var webdriver = require('selenium-webdriver'),
  By = webdriver.By,
  until = webdriver.until;

let unStr='0721tpl';
module.exports = class extends Base {
  indexAction() {
    return this.display();
  }
  async newAction() {
    let driver = await new webdriver.Builder().forBrowser('chrome').usingServer('http://localhost:4444/wd/hub').build();
    global.drives.one=driver;
    await driver.get('https://wwktm058.com');
    // await this.cache('devone',driver, 'memcache');
    console.log(driver);
    // await driver.sleep(2000);
    // console.log(await driver.getPageSource());
    // driver.executeScript('window.frames["index"].document.getElementById("username").value="admin112";window.frames["index"].document.getElementById("passwd").value="123456qq";index.aLeftForm1Sub("0721tpl");');
    // await driver.sleep(2000);
    // driver.executeScript("index.menu_url(1,'"+unStr+"');");
    // //到此登陆成功
    // await driver.sleep(2000);
    // //进入滚球页面
    // driver.executeScript('index.menu_url(2 , "'+unStr+'");')
    // await driver.sleep(3000);
    // //展示滚球数据
    // let iframe1 = driver.findElement(By.id("index"));
    // driver.switchTo().frame(iframe1);
    // await driver.sleep(500);
    // let iframe2 =  driver.findElement(By.id("mainFrame"));
    // driver.switchTo().frame(iframe2);
    // await driver.sleep(1000);
    // driver.findElement(By.xpath('//*[@id="category"]/li[1]')).click();
  }
  async testAction(){
    let action= this.get('action');
    switch(action){
      case 'login':await login();break;
      case 'gogq':await gotGQ();break;
      case 'getOdds':this.body= await getOdds();break;
      case 'bet': await bet();break;
      default:break;
    }
  }
  async test2Action(){
    await login();
    await gotGQ();
    await getOdds();
  }
  async saveAction(){
    await getOdds();
  }
  async getAction(){
    let ret = await this.cache('odds');
    ret=ret?ret:{};
    this.body=ret;
  }
};
let config={
  name:'百分之二',
  url:'https://wwktm058.com',
  username:'admin112',
  passwd:'123456qq'
}
async function login(){
  let driver=await getDriver();
  await driver.get(config.url);
  driver.switchTo().frame(driver.findElement(By.id("index")));
  await driver.sleep(100);
  driver.findElement(By.id('username')).sendKeys(config.username);
  await driver.sleep(200);
  driver.findElement(By.id('passwd')).click();
  await driver.sleep(200);
  driver.findElement(By.id('passwd')).sendKeys(config.passwd);
  driver.findElement(By.xpath('//*[@id="loginbox"]/form/div[2]/input')).click();

  await driver.sleep(1000);
  driver.switchTo().frame(driver.findElement(By.id("index")));
  driver.findElement(By.xpath('//*[@id="content"]/div[2]/div[2]/input[2]')).click();//我同意
  driver.sleep(1000);
}
async function gotGQ(){
  let driver=await getDriver();
  driver.switchTo().frame(driver.findElement(By.id("index")));
  await driver.findElement(By.xpath('//*[@id="blink1"]')).click();

  driver.switchTo().frame(driver.findElement(By.id("mainFrame")));
  driver.findElement(By.xpath('//*[@id="category"]/li[1]/a')).click();//切换滚球
  driver.sleep(1000);
}
async function getOdds(){
  // let driver=await getDriver();
  // driver.switchTo().frame(driver.findElement(By.id("mainFrame")));
  // driver.findElement(By.xpath('//*[@id="reflush_btn"]')).click();//刷新赔率
  // await selenium.WaitForCondition("selenium.browserbot.getCurrentWindow().jQuery.active == 0", "50000");
  // let table1= await driver.findElements(By.className('bian_out'));
  let odds=await think.fetch('https://wwktm058.com/show/ft_gunqiu_data.php?leaguename=&CurrPage=0').then(res=>res.text());
  odds=reconvert(odds);
  odds=odds.substr(1,odds.length-3);
  odds=JSON.parse(odds);
  if(!odds.db) return {};
  let oddsObj={};
  for(let i=0;i<odds.db.length;i++){
    let item = odds.db[i];
    item.Match_Master=(item.Match_Name+'-'+item.Match_Master+'VS'+item.Match_Guest).replace(/\s+/g,"");
    if(!oddsObj[item.Match_Master]) oddsObj[item.Match_Master]={};
    let ggType=item.Match_DxGG;
    let ggTypeH=item.Match_Bdxpk;//半场
    ggType=ggType.replace(/[\u4e00-\u9fa5]/g,"");
    ggTypeH=ggTypeH.replace(/[\u4e00-\u9fa5]/g,"");
    if(ggType)
    oddsObj[item.Match_Master]['全'+ggType]={
      d:Math.round(parseFloat(item.Match_DxDpl)*100)/100,
      x:Math.round(parseFloat(item.Match_DxXpl)*100)/100,
      id:item.Match_ID,
      script:"setbet('足球滚球','大小-小2.5','3411606','Match_DxXpl','1','1','小2.5');"
    }
    if(ggTypeH){
      oddsObj[item.Match_Master]['半'+ggTypeH]={
        d:Math.round(parseFloat(item.Match_Bdpl)*100)/100,
        x:Math.round(parseFloat(item.Match_Bxpl)*100)/100,
        id:item.Match_ID
      }
    }
  }
  await think.cache('odds',oddsObj,{
    timeout: 20 * 1000
  });
  // return oddsObj;
}
async function bet(){
  let driver=await getDriver();
  driver.switchTo().frame(driver.findElement(By.id("mainFrame")));
  driver.executeScript("javascript:setbet('足球滚球','大小-小2','3413932','Match_DxXpl','1','1','小2');");
}
async function getDriver(){//获取浏览器对象
  let driver =global.devOne;
  if(driver==null){
    global.devOne=await new webdriver.Builder().forBrowser('chrome').build();
    return global.devOne;
  }else {
    try{
      await driver.findElement(By.tagName('div'));
      return driver;
    }catch(error){
      global.devOne=await new webdriver.Builder().forBrowser('chrome').build();
      return global.devOne;
    }
  }
}
async function clear(){
  global.devOne=null;
  global.devOne=await new webdriver.Builder().forBrowser('chrome').build();
}
function unicode(str){ 
  return str.replace(/[^\u0000-\u00FF]/g,function($0){return escape($0).replace(/(%u)(\w{4})/gi,"\\u$2")}); 
  }
  function ascii(str){ 
    return str.replace(/[^\u0000-\u00FF]/g,function($0){return escape($0).replace(/(%u)(\w{4})/gi,"\&#x$2;")}); 
    } 

    function reconvert(str){ 
      str = str.replace(/(\\u)(\w{4})/gi,function($0){ 
      return (String.fromCharCode(parseInt((escape($0).replace(/(%5Cu)(\w{4})/g,"$2")),16))); 
      }); 
       
      str = str.replace(/(&#x)(\w{4});/gi,function($0){ 
      return String.fromCharCode(parseInt(escape($0).replace(/(%26%23x)(\w{4})(%3B)/g,"$2"),16)); 
      }); 
      return str; 
      } 